package com.hongye.controller;

import java.util.ArrayList;
import java.util.List;

import com.hongye.app.model.AuthorModel;
import com.hongye.app.model.BuyerAddressModel;
import com.hongye.app.model.BuyerStatisticsModel;
import com.hongye.app.model.GoodsModel;
import com.hongye.app.model.MenuModel;
import com.hongye.app.model.OrderInfoModel;
import com.hongye.app.model.RoleModel;
import com.hongye.app.model.UserAppModel;
import com.hongye.model.AdminUser;
import com.hongye.util.Utils;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

public class UserController extends Controller {
	
	public UserController() {
	}
	
	@Clear
	public void index(){
		renderJsp("index.jsp");
	}
	
	@Clear
	public void login(){
		String retStr="fail";
		if (AdminUser.dao.chkUser(getPara("userid"), getPara("passwd"))) {
			setSessionAttr("userid", getPara("userid"));
			retStr="./main.jsp";
			//TODO 后期需要重构
			String sql = "select au.menuid from admin ad, role ro, auth au where ad.username='"+getPara("userid")+"' and ad.role_id=ro.id and ro.auth_id=au.id";
			String menuid = AuthorModel.dao.findFirst(sql).getStr("menuid");
			String sql2 = "select * from menu where id in "+"("+menuid+")"+" order by pid";
			List<MenuModel> menulist = MenuModel.dao.find(sql2);
			this.setAttr("index", menulist.get(0).get("menu_url"));
			this.setAttr("menulist", menulist);
			renderJsp(retStr);
		}else{
			setSessionAttr("userid", getPara("userid"));
			retStr="index.jsp";
			renderJsp(retStr);
		}
	}
	
	/**
	 * 用户管理（得到用户列表）-管理端
	 * @author lifuqiang
	 * 2016-10-6
	 */
	public void userView(){
		String sql = "select * from buyer b, buyer_statistics bs where b.phone = bs.buyer_phone order by r_time desc";
		List<UserAppModel> userview =UserAppModel.dao.find(sql);
		this.setAttr("userview", userview);
		this.renderJsp("userView.jsp");
	}
	
	/**
	 * 用户管理（得到被操作的用户信息）-管理端
	 * @author lifuqiang
	 * 2016-10-8
	 */
	public void userDetailView(){
		String phone = this.getPara("phone");
		String sql = "select * from buyer where phone = '"+phone+"'";		
		String sql2 = "select * from buyer_address where user_phone = '"+phone+"'";		
		UserAppModel userdetail = UserAppModel.dao.findFirst(sql);
		List<BuyerAddressModel> addresslist = BuyerAddressModel.dao.find(sql2);		
		int userid = userdetail.getInt("user_id");
		String sql3 = "select * from goods where seller_id='"+userid+"'";
		List<GoodsModel> goodslist = GoodsModel.dao.find(sql3);
		this.setAttr("userdetail", userdetail);
		this.setAttr("addresslist", addresslist);
		this.setAttr("goodslist", goodslist);
		this.setAttr("id", userdetail.get("user_id"));
		this.renderJsp("userDetail.jsp");
	}
	
	/**
	 * 用户管理（修改用户状态）-管理端
	 * @author lifuqiang
	 * 2016-10-8
	 */
	public void changeUserState(){
		String user_id = this.getPara("id");
		String state = this.getPara("state");
		String sql = "select * from buyer where user_id= '"+user_id+"'";
		UserAppModel user = UserAppModel.dao.findFirst(sql);
		String rs= null;
		if(user.set("status", state).update()){
			rs = "suc";
		}else{
			rs = "fal";
		}
		this.renderText(rs);
	}
	
	/**
	 * 用户管理（得到被操作的用户的店铺信息）-管理端
	 * @author lifuqiang
	 * 2017-3-7
	 */
	public void userShopDetailView(){
		String phone = this.getPara("phone");
		String sql = "select * from buyer_statistics where buyer_phone = '"+phone+"'";
		BuyerStatisticsModel bs = BuyerStatisticsModel.dao.findFirst(sql);
		this.setAttr("shopdetail", bs);
		this.setAttr("phone", phone);
		this.renderJsp("userShopDetail.jsp");
	}
	
	/**
	 * 用户管理（修改用户店铺状态）-管理端
	 * @author lifuqiang
	 * 2016-10-8
	 */
	public void changeShopState(){
		String phone = this.getPara("phone");
		String state = this.getPara("state");
		String sql = "select * from buyer_statistics where buyer_phone= '"+phone+"'";
		BuyerStatisticsModel bs = BuyerStatisticsModel.dao.findFirst(sql);
		String rs= null;
		if(bs.set("isshop", state).update()){
			rs = "suc";
		}else{
			rs = "fal";
		}
		this.renderText(rs);
	}
	
	/**
	 * 用户平台管理-管理端
	 * @author liushaomin
	 * 2016-10-7
	 */
	public void adminUserView(){
		String sql = "select * from admin";
		List<AdminUser> adminview = AdminUser.dao.find(sql);
		for (AdminUser item : adminview) {
			RoleModel role = RoleModel.dao.findById(item.get("role_id"));
			item.put("rolename", role.get("r_name"));
		}
		List<RoleModel> role = RoleModel.dao.find("select * from role");
		this.setAttr("rolelist", role);
		this.setAttr("adminUserView", adminview);
		this.renderJsp("adminUserView.jsp");
	}
	
	/**
	 * 查看平台用户 详情（可以修改状态）
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void adminDetail(){
		String id = this.getPara("id");
		AdminUser adminUser = AdminUser.dao.findById(id);
		List<RoleModel> role = RoleModel.dao.find("select * from role");
		this.setAttr("rolelist", role);
		this.setAttr("roleid", adminUser.getInt("role_id"));
		this.setAttr("adminUser", adminUser);
		this.setAttr("id", adminUser.get("id"));
		this.renderJsp("adminUserDetail.jsp");
	}
	
	/**
	 * 修改平台用户状态（冻结、解冻）
	 * 修改平台用户
	 * 修改用户名/状态（解冻、冻结）/角色
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void editAdmin(){
		String id = this.getPara("id");
		String state = this.getPara("state");
		String username = this.getPara("username");
		String role_id = this.getPara("role_id");
		AdminUser admin = AdminUser.dao.findById(id);
		String rs= "fal";
		if(!state.equals("undefined")){
			if(admin.set("username", username).set("role_id", role_id).set("status", state).update()){
				rs = "suc";
			};
		}else{
			if(admin.set("username", username).set("role_id", role_id).update()){
				rs = "suc";
			};
		}
		this.renderText(rs);
	}
	
	/**
	 * 添加平台用户
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void addAdminUser(){
		String username = this.getPara("username");
		String password = this.getPara("password");
		String roleid = this.getPara("roleid");
		String sql = "select * from admin where username = '"+ username +"'";
		List<AdminUser> adminuser = AdminUser.dao.find(sql);
		String rs= "exist";
		if(adminuser.size() == 0){
			AdminUser admin = new AdminUser();
			boolean boo = admin.set("username", username).set("password", Utils.strToMD5(password)).set("c_time", Utils.getDate()).set("role_id", roleid).set("status", 0).save();
			if(boo){
				rs = "suc";
			}else{
				rs = "fal";
			}
		}
		this.renderText(rs);
	}
	
	/**
	 * 删除平台用户
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void delAdminUser(){
		String id = this.getPara("id");
		boolean boo = AdminUser.dao.deleteById(id);
		String rs= "fal";
		if (boo) {
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 角色列表
	 * @author lifuqiang
	 * 2016-10-13
	 */
	public void roleView(){
		String sql = "select * from role";
		String sql2 = "select * from auth order by level";
		List<RoleModel> rolelist = RoleModel.dao.find(sql);
		List<AuthorModel> authlist = new ArrayList<AuthorModel>();
		List<AuthorModel> authorlist = AuthorModel.dao.find(sql2);
		for(RoleModel ro : rolelist){
			authlist.add(AuthorModel.dao.findById(ro.get("auth_id")));
		}
		this.setAttr("rolelist", rolelist);
		this.setAttr("authlist", authlist);
		this.setAttr("authorlist", authorlist);
		this.renderJsp("roleView.jsp");
	}
	
	/**
	 * 添加角色
	 * @author lifuqiang
	 * 2016-10-13
	 */
	public void addRole(){
		String rolename = this.getPara("rolename");
		String authname = this.getPara("authname");
		String rs= "exist";
		String sql = "select * from auth where auth_name='"+authname+"'";
		String sql2 = "select * from role where r_name='"+rolename+"'";
		AuthorModel author = AuthorModel.dao.findFirst(sql);
		List<RoleModel> rolelist = RoleModel.dao.find(sql2);
		if(rolelist.size()==0){
			int authid = author.getInt("id");
			if(RoleModel.dao.set("r_name", rolename).set("auth_id", authid).save()){
				rs = "suc";
			}else{
				rs = "fal";
			}
		}		
		this.renderText(rs);
		
	}
	
	/**
	 * 编辑角色-详情
	 * @author lifuqiang
	 * 2016-10-13
	 */
	public void roleDetail(){
		String id = this.getPara("id");
		RoleModel roledetail = RoleModel.dao.findById(id);
		AuthorModel auth = AuthorModel.dao.findById(roledetail.get("auth_id"));
		String authname = auth.getStr("auth_name");
		String sql = "select * from auth order by level";
		List<AuthorModel> authorlist = AuthorModel.dao.find(sql);
		this.setAttr("authname", authname);
		this.setAttr("roledetail", roledetail);
		this.setAttr("authorlist", authorlist);
		this.setAttr("id", roledetail.get("id"));
		this.renderJsp("roleDetail.jsp");
	}
	
	/**
	 * 编辑更新角色
	 * @author lifuqiang
	 * 2016-10-13
	 */
	public void editRole(){
		String id = this.getPara("id");
		String rolename = this.getPara("rolename");
		String authname = this.getPara("authname");
		String sql = "select * from auth where auth_name='"+authname+"'";
		RoleModel rolemodel = RoleModel.dao.findById(id);
		AuthorModel author = AuthorModel.dao.findFirst(sql);
		String rs= "fal";
		if(rolemodel.set("r_name", rolename).set("auth_id", author.get("id")).update()){
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 删除角色
	 * @author lifuqiang
	 * 2016-10-13
	 */
	public void removeRole(){
		String id = this.getPara("roleid");
		String rs = "fal";
		boolean isremove = RoleModel.dao.deleteById(id);
		if(isremove){
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 权限列表
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void authView(){
		String sql = "select * from auth";
		List<AuthorModel> auth = AuthorModel.dao.find(sql);
		List<MenuModel> menus = MenuModel.dao.find("select * from menu");
		this.setAttr("authView", auth);
		this.setAttr("menus", menus);
		this.renderJsp("authView.jsp");
	}
	
	/**
	 * 编辑权限
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void authDetail(){
		String id = this.getPara("id");
		AuthorModel auth = AuthorModel.dao.findById(id);
		List<MenuModel> menus = MenuModel.dao.find("select * from menu");
		this.setAttr("menus", menus);
		this.setAttr("auth", auth);
		this.setAttr("id", auth.get("id"));
		this.setAttr("menuid", auth.get("menuid"));
		this.renderJsp("authDetail.jsp");
	}
	
	/**
	 * 添加权限
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void addAuth(){
		String auth_name = this.getPara("auth_name");
		String auth_c = this.getPara("auth_c");
		String auth_a = this.getPara("auth_a");
		String level = this.getPara("level");
		String menuids = this.getPara("menuids");
		String sql = "select * from auth where auth_name='"+auth_name+"'";
		List<AuthorModel> author = AuthorModel.dao.find(sql);
		String rs= "exist";
		if(author.size() == 0){
			boolean boo = new AuthorModel().set("auth_name", auth_name).set("auth_c", auth_c).set("auth_a", auth_a).set("level", level).set("menuid", menuids).save();
			if(boo){
				rs = "suc";
			}else{
				rs = "fal";
			}
		}
		this.renderText(rs);
	}
	
	/**
	 * 删除权限
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void delAuth(){
		String id = this.getPara("id");
		boolean boo = AuthorModel.dao.deleteById(id);
		String rs= "fal";
		if (boo) {
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 提交 编辑权限
	 * @author liushaomin
	 * 2016年10月13日
	 */
	public void editAuth(){
		String id = this.getPara("id");
		String auth_name = this.getPara("auth_name");
		String auth_c = this.getPara("auth_c");
		String auth_a = this.getPara("auth_a");
		String level = this.getPara("level");
		String menuid = this.getPara("menuid");
		AuthorModel auth = AuthorModel.dao.findById(id);
		boolean boo = auth.set("auth_name", auth_name).set("auth_c", auth_c).set("auth_a", auth_a).set("level", level).set("menuid", menuid).update();
		String rs= "fal";
		if (boo) {
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 菜单列表
	 * @author lifuqiang
	 * 2016-10-14
	 */
	public void menuView(){
		String sql = "select * from menu";
		List<MenuModel> menulist = MenuModel.dao.find(sql);
		this.setAttr("index", menulist.get(0).get("menu_url"));
		this.setAttr("menulist", menulist);
		this.renderJsp("menuView.jsp");
	}
	
	/**
	 * 添加菜单
	 * @author lifuqiang
	 * 2016-10-14
	 */
	public void addMenu(){
		String menuname = this.getPara("menuname");
		String pid = this.getPara("pid");
		String url = this.getPara("url");
		String sql = "select * from menu where menu_name='"+menuname+"'";
		String sql2 = "select * from menu";
		List<MenuModel> menu = MenuModel.dao.find(sql);
		List<MenuModel> menulist = MenuModel.dao.find(sql2);
		String rs = "exist";
		if(menu.size()==0){
			int size = menulist.size();
			boolean isadd = new MenuModel().set("menu_name", menuname).set("pid", pid).set("sort", size+1).save();
			if(isadd){
				System.err.println("-----------"+url.length());
				if(url.length() != 0){
					MenuModel mm = MenuModel.dao.findFirst(sql);
					boolean boo = mm.set("menu_url", url).update();
					if(boo){
						rs = "suc";
					}else{
						rs = "fal";
					}
				}else{
					rs = "suc";
				}
			}else{
				rs = "fal";
			}
		}
		this.setAttr("state", rs);
		this.renderText(rs);
	}
	
	/**
	 * 编辑菜单-详情
	 * @author lifuqiang
	 * 2016-10-14
	 */
	public void menuDetail(){
		String id = this.getPara("id");
		String sql = "select * from menu where id = '"+id+"'";
		MenuModel menu = MenuModel.dao.findFirst(sql);
		this.setAttr("menudetail", menu);
		this.setAttr("id", menu.get("id"));
		this.renderJsp("menuDetail.jsp");
	}
	
	/**
	 * 提交修改-编辑菜单
	 * @author lifuqiang
	 * 2016-10-14
	 */
	public void editMenu(){
		String id = this.getPara("id");
		String menuname = this.getPara("menuname");
		String pid = this.getPara("pid");
		String url = this.getPara("url");
		String sql = "select * from menu where id='"+id+"'";
		MenuModel menu = MenuModel.dao.findFirst(sql);
		String rs = "fal";
		if(menu.set("menu_name", menuname).set("pid", pid).set("menu_url", url).update()){
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 删除菜单
	 * @author lifuqiang
	 * 2016-10-14
	 */
	public void removeMenu(){
		String id = this.getPara("id");
		String rs= "fal";
		if (MenuModel.dao.deleteById(id)) {
			rs = "suc";
		}
		this.renderText(rs);
	}
	
	/**
	 * 综合统计（用户量，订单量，商品量，交易量）
	 * @author lifuqiang
	 * 2017-4-20
	 */
	public void statisticalView(){
		long userNumber = UserAppModel.userNumber();//注册用户量
		long openshopNumber = BuyerStatisticsModel.OpenShopNumber();//用户开通店铺量
		long orderNumber = OrderInfoModel.orderNumber();//订单成交量
		double moneyNumber = OrderInfoModel.moneynumber();//交易额
		long goodsNumber = GoodsModel.goodsNumber();//商品上传量
		this.setAttr("userNumber", String.valueOf(userNumber));
		this.setAttr("openshopNumber", String.valueOf(openshopNumber));
		this.setAttr("orderNumber", String.valueOf(orderNumber));
		this.setAttr("moneyNumber", String.valueOf(moneyNumber));
		this.setAttr("goodsNumber", String.valueOf(goodsNumber));
		this.setAttr("shopRatio", String.valueOf((openshopNumber*50)/userNumber));
		this.renderJsp("statisticalView.jsp");
	}
	
	
}
